{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "高数笔记整理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " import sympy as sp\n",
    " from sympy import symbols,cos,sin,pi,Eq,solve,lambdify,Function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x,y,z=sp.symbols('x,y,z')\n",
    "expr=cos(x)+sin(pi/2)\n",
    "expr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "高等数学上册第18页数列的极限"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.数列的极限\n",
    "SymPy 可以轻松计算极限\n",
    "limx→0cos(x)/2x\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import symbols, limit, cos\n",
    "\n",
    "# 定义符号变量\n",
    "x = symbols('x')\n",
    "\n",
    "# 定义一个函数，例如：cos(x)/2x\n",
    "f = cos(x) / 2x\n",
    "\n",
    "# 计算当x趋近于0时的极限\n",
    "lim = limit(f, x, 0)\n",
    "\n",
    "print(lim)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "高等数学下册第72页偏导数的计算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.微分的计算与应用\n",
    "sympy可以轻松计算微分\n",
    "z = x ** 2 * sp.sin(y)，要求它关于 x 和 y 的偏导数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x, y = sp.Symbol('x'), sp.Symbol('y')\n",
    "z = x ** 2 * sp.sin(y)\n",
    "# 求z关于x的偏导数\n",
    "partial_z_x = sp.diff(z, x)\n",
    "print(partial_z_x)\n",
    "\n",
    "# 求z关于y的偏导数\n",
    "partial_z_y = sp.diff(z, y)\n",
    "\n",
    "print(partial_z_y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求函数 y = sp.exp(x) 的二阶导数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = sp.Symbol('x')\n",
    "y = sp.exp(x)\n",
    "second_derivative = sp.diff(y, x, 2)\n",
    "print(second_derivative)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "高等数学下册第259页常数项级数的审敛法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.级数判断收敛\n",
    "（1）. 安装与导入\n",
    "首先确保已经安装了sympy库（若未安装，可通过pip install sympy命令安装），然后在代码中导入相关的模块和函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sp\n",
    "from sympy.abc import n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（2）.判断级数收敛。如级数 a_n = (2 * n + 1) / (3 * n ** 2) 的收敛性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sp\n",
    "from sympy.abc import n\n",
    "\n",
    "a_n = (2 * n + 1) / (3 * n ** 2)\n",
    "limit_root = sp.limit(sp.root(a_n, n), n, sp.oo)\n",
    "if limit_root < 1:\n",
    "    print(\"根据根值判别法，该级数收敛\")\n",
    "elif limit_root > 1:\n",
    "    print(\"根据根值判别法，该级数发散\")\n",
    "elif limit_root == 1:\n",
    "    print(\"根值判别法无法确定该级数收敛性，需尝试其他判别法\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "高等数学下册第135页二重积分的计算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.二重积分的计算\n",
    "对f(x,y) = x^2 + y^2进行积分，假设积分区域是由直线 y = 0，x = 1 和 y = x 围成的三角形区域。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（1）定义变量与被积函数\n",
    "先导入模块、定义变量和被积函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sp\n",
    "from sympy.abc import x, y\n",
    "\n",
    "# 定义被积函数\n",
    "f = x ** 2 + y ** 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(2)确定积分限并计算二重积分\n",
    "先对 y 积分时，y 的下限是 0，上限是 x（由 y = x 这条边界确定）；然后对 x 积分，x 的下限是 0，上限是 1。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 先对y积分，积分限是从0到x\n",
    "result_y = sp.integrate(f, (y, 0, x))\n",
    "\n",
    "# 再对x积分，积分限是从0到1\n",
    "result = sp.integrate(result_y, (x, 0, 1))\n",
    "\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.定积分\n",
    "当Integrate()函数第二个参数传入积分上下界时，默认计算表达式在给定范围内的定积分：\n",
    "例：求定积分∫log20exdx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sp\n",
    "\n",
    "# 定义变量x\n",
    "x = sp.Symbol('x')\n",
    "\n",
    "# 定义被积函数\n",
    "expr = sp.exp(x**2)\n",
    "\n",
    "# 计算定积分\n",
    "integral_result = sp.integrate(expr, (x, 0, sp.log(2)))\n",
    "\n",
    "print(integral_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "线性代数第一章矩阵的乘法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6.矩阵的乘法\n",
    "（1）创建两个可相乘的矩阵\n",
    "要进行矩阵与矩阵相乘，需要满足前一个矩阵的列数等于后一个矩阵的行数。例如，创建一个 2×3 的矩阵 matrix_a 和一个 3×2 的矩阵 matrix_b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "matrix_a = sp.Matrix([[1, 2, 3], [4, 5, 6]])\n",
    "matrix_b = sp.Matrix([[7, 8], [9, 10], [11, 12]])\n",
    "print(matrix_a)\n",
    "print(matrix_b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（2）进行矩阵与矩阵的乘法运算\n",
    "使用 sympy 库中的乘法运算规则来计算两个矩阵的乘积："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = matrix_a * matrix_b\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "matrix_a = sp.Matrix([[1, 2, 3], [4, 5, 6]])\n",
    "matrix_b = sp.Matrix([[7, 8], [9, 10], [11, 12]])\n",
    "print(matrix_a)\n",
    "print(matrix_b)\n",
    "result = matrix_a * matrix_b\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "高等数学上册第13页洛必达法则"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "7.洛必达法则求导\n",
    "（1）“0/0” 型未定式极限实例\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "先构造出函数 f(x) = \\frac{x^3 - 1}{x^2 - 1}\\)，当 x \\to 1时它是“\\(0/0\\)”型，然后对分子分母分别求导，分子求导后为\\(3x^2\\)，分母求导后为\\(2x\\)，新的函数new_f就是\\(\\frac{3x^2}{2x}\\)，化简后为\\(\\frac{3x}{2}\\)，再求当x \\to 1` 时的极限，运行代码后输出结果为3/2。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = sp.Symbol('x')\n",
    "f = (x^3 - 1) / (x^2 - 1)\n",
    "# 对分子分母分别求导\n",
    "new_f = sp.diff(f.numerator, x) / sp.diff(f.denominator, x)\n",
    "# 求导后的函数求极限\n",
    "result = sp.limit(new_f, x, 1)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（2）“∞/∞型未定式极限实例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "while True:\n",
    "    new_f = sp.diff(f.numerator, x) / sp.diff(f.denominator, x)\n",
    "    # 判断求导后的函数是否还是未定式\n",
    "    if sp.limit(new_f, x, +\\infty) == sp.oo or sp.limit(new_f, x, +\\infty) == 0:\n",
    "        f = new_f\n",
    "    else:\n",
    "        break\n",
    "\n",
    "result = sp.limit(f, x, +\\infty)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上述代码中，进入循环后先对分子分母求导得到 new_f，然后判断求导后的函数 new_f 在 x \\to +\\infty 时是否还是未定式（“0/0” 型或 “∞/∞” 型），如果是则继续更新 f 为 new_f 并再次循环求导求极限，直到求导后的函数不再是未定式，最后跳出循环求此时 f 的极限。对于这个例子，经过两次应用洛必达法则后（第一次求导后函数还是 “∞/∞” 型，继续求导），最终可以得到极限值为 0。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
